//
//  CityViewController.m
//  团购
//
//  Created by 樊荣海 on 15/1/21.
//  Copyright (c) 2015年 樊荣海. All rights reserved.
//

#import "CityViewController.h"




#define searchBarH 44

@interface CityViewController () <UITableViewDataSource, UITableViewDelegate, UISearchBarDelegate, UISearchDisplayDelegate>

@end

@implementation CityViewController {
    
    UITableView *_tableView;
    UISearchBar *_bar;
    UISearchDisplayController *_displayVC;
    
    NSMutableArray *_citySecionmodels;//所有citySection组

    NSMutableArray *_resultArray;//所有搜索结果
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //可以在popover设置, 也可以在此处设置
    //self.preferredContentSize = CGSizeMake(320, 480);
    
    //1.加载数据
    [self initData];
    
    //2.tableView
    [self addTableView];
    
    //3.添加搜索框
    [self addSearchBar];
    
    //4.[重要]监听键盘--旋转屏幕会调用此通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil];

//     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenRotate) name:UIDeviceOrientationDidChangeNotification object:nil];
    
    
    //5. searchDisplay
    _displayVC = [[UISearchDisplayController alloc] initWithSearchBar:_bar contentsController:self];
    [_displayVC setDelegate:self];
    [_displayVC setSearchResultsDataSource:self];
    [_displayVC setSearchResultsDelegate:self];
}

- (void)addSearchBar {

    _bar = [[UISearchBar alloc] init];
    _bar.delegate = self;
    //[重要]搜索框的宽度768, 设置auto!
    _bar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    _bar.frame = CGRectMake(0, 0, self.view.frame.size.width, searchBarH);
    _bar.placeholder = @"请输入城市名或拼音";
//    _bar.tintColor = [UIColor redColor];//光标颜色
    [self.view addSubview:_bar];

}

- (void)addTableView {
    
    _tableView = [[UITableView alloc] init];
    _tableView.frame = CGRectMake(0, searchBarH, self.view.frame.size.width, self.view.frame.size.height-searchBarH);
    _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];

}

- (void)initData {
    
    _citySecionmodels = [NSMutableArray arrayWithArray:[[MetaDataTool sharedMetaDataTool] totalCitySections]];
    
    _resultArray = [NSMutableArray array];
}

//数据
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    if (tableView == _tableView) {
        return _citySecionmodels.count;
    } else if (tableView == _displayVC.searchResultsTableView) {
        return 1;
    } else {
        return 0;
    }

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (tableView == _tableView) {
        CitySectionModel *citySection = _citySecionmodels[section];
        
        return [citySection.cities count];
        
    } else if (tableView == _displayVC.searchResultsTableView) {

        return _resultArray.count;
        
    } else {
        return 0;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (tableView == _tableView) {
        static NSString *ID = @"tableView_Cell";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
        
        CitySectionModel *citySection = _citySecionmodels[indexPath.section];
        
        CityModel *city = citySection.cities[indexPath.row];
        
        cell.textLabel.text = city.name;
        return cell;
        
    } else if (tableView == _displayVC.searchResultsTableView) {
        
        static NSString *ID = @"tableView_Cell";
        
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        }
        
        CityModel *city = _resultArray[indexPath.row];
    
        cell.textLabel.text = city.name;
        return cell;
        
    } else {
        return nil;
    }
    
    
    

}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {

    if (tableView == _tableView) {
        CitySectionModel *citySection = _citySecionmodels[section];
        return citySection.name;
    } else if (tableView == _displayVC.searchResultsTableView) {
        return @"搜索结果";
    } else {
        return nil;
    }
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {

    if (tableView == _tableView) {
        //[技巧]
        return [_citySecionmodels valueForKey:@"name"];
    } else if (tableView == _displayVC.searchResultsTableView) {
        return nil;
    } else {
        return nil;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CityModel *city;
    
    if (tableView == _tableView) {
        CitySectionModel *citySection = _citySecionmodels[indexPath.section];
        city = citySection.cities[indexPath.row];
        
    } else if (tableView == _displayVC.searchResultsTableView) {
        
        city= _resultArray[indexPath.row];
        
    } else {
        
        
        
    }
    
    //插入数据库
    [[DBTool sharedDBTool] insertCityModel:city];
    
    //改变元数据
    [[MetaDataTool sharedMetaDataTool] updateVisitedCity];
    
    //清空所选的分类
    [[DBTool sharedDBTool] deleteAllSubAndSuperTitle];
    
    //发出改变topBtn 通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"removeAllMenuView" object:nil];
    
    //发出通知
    [[NSNotificationCenter defaultCenter] postNotificationName:kCityChangeNote object:city];
}




//searchDelegate
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    
    NSLog(@"searchText: %@", searchText);
    
    [_resultArray removeAllObjects];
    [_resultArray addObjectsFromArray:[[MetaDataTool sharedMetaDataTool] citymodelsWithString:searchText]];

    [_displayVC.searchResultsTableView reloadData];

}

//失去焦点
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {
    NSLog(@"失去焦点");
//     [self coverClick];
}

#warning mark 此处有大疑问--旋转屏幕会触发键盘隐藏的通知--待解决
- (void)keyboardWillHide {
    NSLog(@"键盘回收了");
    
}

- (void)screenRotate {
    NSLog(@"city 旋转");
    
    
    
}


@end
